<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="classId">
        <SearchCourseName ref="SearchCourseName" @update="updateCourseInfo" />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentId">
        <SearchStudent ref="SearchStudent" @change="updateStudentInfo"/>
      </el-form-item>
      <el-form-item label="销售人员" prop="courseSalesman">
        <el-input
          v-model="queryParams.courseSalesman"
          placeholder="请输入销售人员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['school:course:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['school:course:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['school:course:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['school:course:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="课时" align="center" prop="classTime" />
      <el-table-column label="剩余课时" align="center" prop="classSurplusTime" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="课程开始时间" align="center" prop="startTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="定金" align="center" prop="deposit">
        <template slot-scope="scope">
          <span>{{formatPrice(scope.row.deposit)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="总金额" align="center" prop="totalAmount">
        <template slot-scope="scope">
          <span>{{formatPrice(scope.row.totalAmount)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售人员" align="center" prop="courseSalesman">
        <template slot-scope="scope">
          <div>{{scope.row.courseSalesman ? scope.row.courseSalesman : '- -'}}</div>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <div v-if="!scope.row.payType">- -</div>
          <dict-tag v-else :options="dict.type.sys_pay_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['school:course:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['school:course:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <CourseModel ref="CourseModel" @updatedata="updateClassData" />
  </div>
</template>

<script>
import { listCourse, getCourse, delCourse, addCourse, updateCourse, exportCourse } from "@/api/school/course";
import CourseModel from '../model/courseModel.vue'
import SearchCourseName from '../model/searchCourseName.vue'
import SearchStudent from '../model/searchStudent.vue'
export default {
  name: "Course",
  dicts: ['sys_pay_type'],
  components: {CourseModel,SearchCourseName, SearchStudent},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生购买课程表格数据
      courseList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classId: null,
        studentId: null,
      },
      form: {},
    };
  },
  computed: {
    formatPrice() {
      return (val) => {
          return String(val).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    updateCourseInfo(e) {
      this.queryParams.classId = e
    },
    updateStudentInfo(e) {
      this.queryParams.studentId = e
    },
    updateClassData() {
      this.getList();
    },
    /** 查询学生购买课程列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.$refs.SearchCourseName.init()
      this.$refs.SearchStudent.init()
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.CourseModel.reset();
      this.$refs.CourseModel.open = true;
      this.$refs.CourseModel.title = "添加学生购买课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.CourseModel.reset();
      console.log(row);
      const id = row.id || this.ids
      getCourse(id).then(response => {
        this.$refs.CourseModel.form = response.data;
        this.$refs.CourseModel.open = true;
        this.$refs.CourseModel.title = "修改学生购买课程";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中学生购买课程编？').then(function() {
        return delCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有学生购买课程数据项？').then(() => {
        this.exportLoading = true;
        return exportCourse(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
